package org.brisling.honest.queryService;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.brisling.authorize.shiro.ShiroSecurityService;
import org.brisling.base.domain.ChartDomain;
import org.brisling.base.domain.ListDomain;
import org.brisling.base.service.AbstractBaseQueryService;
import org.brisling.common.ConstSysParam;
import org.brisling.common.SysConfig;
import org.brisling.common.util.LongUtil;
import org.brisling.common.util.WhereUtil;
import org.brisling.duties.queryService.QueryDutiesinquiryService;
import org.brisling.honest.domain.HonestDomain;
import org.brisling.medical.queryService.QueryRecipelService;
import org.brisling.satisfaction.queryService.QuerySatisRankService;
import org.brisling.supply.queryService.QuerySupplyinquiryService;
import org.brisling.sysman.queryService.QueryDictionaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

@RestController
@RequestMapping("/honest/query")
public class QueryHonestService extends AbstractBaseQueryService<HonestDomain>{
	
	private HashMap<String,String> typeMap =null;
	
	private HashMap<String,String> orderMap =null;
	
	@Autowired
	private QueryDutiesinquiryService qryDutiesService;
	
	
	@Autowired
	private QueryRecipelService qryMedicalService;
	
	@Autowired
	private QuerySatisRankService qrySatisRankService;
	
	@Autowired
	private ShiroSecurityService shiroSecurityService;
	
	@Autowired
	private QuerySupplyinquiryService qrySupplyService;
	@Autowired
	private QueryDictionaryService dicService;
	public QueryHonestService(){
		this.moduleName = "honest";
		this.subSystem = "honest";
		this.tableName = ConstSysParam.VIEW_PREFIX +"honest";
		this.clazz = HonestDomain.class;
		
		typeMap = new HashMap<String,String>();
			typeMap.put("dept_name",ConstSysParam.STRING);
			typeMap.put("doct_depart_code",ConstSysParam.STRING);
		
		
		orderMap = new HashMap<String,String>();
		orderMap.put("sumary_matter","desc");
			
		
	}
	
	@PersistenceContext(unitName=ConstSysParam.SUBSYSTEM_SYSMAN)
	@Autowired
	protected void setEntityManager(EntityManager entityManager){
		this.entityManager = entityManager;		
	}
	
	@RequestMapping("/mainPage.action")
	public ModelAndView getMainPage(){
		return new ModelAndView("/sysman/chart_all/main");
	}
	
	/* (non-Javadoc)
	 * @see org.brisling.service.sysman.DepartmentServiceInf#getEditPage()
	 */
	@RequestMapping("/editPage.action")
	public ModelAndView getEditPage(){
		return new ModelAndView("/sysman/chart_all/main");
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping("/getListDomain.action")
	public ListDomain<HonestDomain> getListDomain(
			@RequestBody(required=false) String body){		
		
		String sql = dicService.getSqlByBizcode("duties_biz_statistic");
		String userid = shiroSecurityService.getCurrentUserIdentify();
		Map<String,Object> map = this.getBodyParam(body);
		
		Map<String,String> conditionMap = (Map<String, String>) map.get("condition");		
		
		if(!this.isSuperDept(userid,"query_all", "query_duties_all"))		
			conditionMap.put("doct_depart_code", SysConfig.getUsr(userid).getDeptid().toString());
		
		String whereCorse = WhereUtil.getConditionWhereCorse(conditionMap,typeMap);		
		whereCorse = (whereCorse==null)||(whereCorse.trim().length()<=0)?"":" where " + whereCorse;
		
		sql = String.format(sql, whereCorse);
		Integer page = map.containsKey("page")?LongUtil.getIntValue(map.get("page").toString(),ConstSysParam.PAGE):ConstSysParam.PAGE;
		Integer pageSize = map.containsKey("rows")?LongUtil.getIntValue(map.get("rows").toString(),ConstSysParam.ROWS):ConstSysParam.ROWS;
		return super.getPagedListDomain(page, pageSize, sql);		
		
	}
	
	
	
	@RequestMapping("/getList.action")
	public List<HonestDomain> getList(@RequestParam(value="page",required=false) Integer page,
			@RequestParam(value="rows",required=false) Integer rows,
			@RequestParam(value="deptid",required=false) Integer deptid,
			@RequestParam(value="sort",required=false) String sort){		
		
		StringBuilder sb = new StringBuilder();
		String sql = "";
		if(deptid!=null){
			String whereCorse=String.format(sb.append("\"deptid\" = %1$d").toString(),deptid);
			sql = String.format(this.TW_SQL, this.tableName,whereCorse);
		}else{
			sql = String.format(this.T_SQL, this.tableName);
		}
		
		return super.getPagedList(page,rows,sql);
	}
	
	
	@RequestMapping("/getStaticInfoAll.action")
	public LinkedHashMap<String,List<ChartDomain>> getStaticInfoAll(@RequestBody(required=false) String body){
		LinkedHashMap<String,List<ChartDomain>> map = new LinkedHashMap<String,List<ChartDomain>>(); 
		map.put("duties", qryDutiesService.getTaskCnt(body));
		map.put("medical", qryMedicalService.getTaskCnt(body));
//		map.put("satisfaction", qrySatisRankService.getTaskCnt(body));
//		map.put("qrySupplyService", qrySatisRankService.getTaskCnt(body));
		
		map.put("satisfaction", qrySatisRankService.getTaskCnt(body));
		map.put("supply", qrySupplyService.getTaskCnt(body));
		return map;
	}
	/**
	 * 根据用户id查找用户信息
	 * @param id	用户id
	 * @return 用户信息记录 
	 */
	@RequestMapping("/getOne.action")
	public HonestDomain getOne(@RequestParam(value="id",required=false) Long id){
		return super.getOne(id);
	}
	
	/**
	 * 根据用户唯一编码查找用户信息
	 * @param identify	用户唯一编码
	 * @return 用户信息记录
	 */
	@RequestMapping("/getOneByIdentify.action")
	public HonestDomain getOneByIdentify(@RequestParam(value="identify",required=false) String identify){	
		return super.getOneByIdentify(identify);
	}
	
	
}
